﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Win32;

namespace VideoChatClient
{
	public partial class Page_Settings
	{
		public Page_Settings()
		{
			this.InitializeComponent();
            DataContext = App.ViewModel;
            App.ViewModel.DeviceControl.evFirm += new SelectorDevice.DeviceExch.StatusFirm(DeviceControl_evFirm);
			// Insert code required on object creation below this point.
		}

        void DeviceControl_evFirm(double Progress)
        {
            this.Dispatcher.BeginInvoke(
                DispatcherPriority.Normal,
                (ThreadStart) delegate()
                                  {
                                      pb_Progress.Value = Progress*100;
                                      if(Progress==255)
                                      {
                                          pb_Progress.Visibility = System.Windows.Visibility.Collapsed;
                                      }
                                  });
            //throw new NotImplementedException();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.Save();
            NavigationService.Navigate(new Page_ClientMain());
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.Reload();
            NavigationService.Navigate(new Page_ClientMain());
        }

        private void Page_Unloaded(object sender, System.Windows.RoutedEventArgs e)
        {
            Properties.Settings.Default.Reload();
        }

        private void btn_UpdateFirm_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var ofd = new OpenFileDialog();

            if(ofd.ShowDialog()==true)
            {
                pb_Progress.Value = 0;
                pb_Progress.Visibility = System.Windows.Visibility.Visible;
                App.ViewModel.DeviceControl.FuncUpdateFirmware(ofd.FileName);
            }
        	// TODO: Add event handler implementation here.
        }
	}
}